package cn.net.vote.data.controller;

import cn.net.vote.data.domain.PageData;
import cn.net.vote.data.domain.ResponseBodyBean;
import cn.net.vote.data.service.IActivityService;
import cn.net.vote.util.TimeUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 【描 述】：<活动管理模块控制层>
 * 【表 名】：<数据库表名称>
 * 【环 境】：J2SE 1.8

 * @version  version 1.0
 * @since    2018年9月11日
 */
@Api(description = "活动管理", value = "/")
@RestController
@EnableAutoConfiguration
@RequestMapping(value="/activity")
public class ActivityController extends BaseController{

    @Autowired
    private IActivityService activityService;

    @ApiOperation(value = "查看活动详情", notes = "查看活动详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "pd", dataType = "PageData", required = true, value = "客户端传入JSON字符串", defaultValue = "")  })
    @ApiResponses({
            @ApiResponse(code=200,message="指示客服端的请求已经成功收到，解析，接受"),
            @ApiResponse(code=201,message="资源已被创建"),
            @ApiResponse(code=401,message="未授权"),
            @ApiResponse(code=400,message="请求参数没填好"),
            @ApiResponse(code=403,message="拒绝访问"),
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对"),
            @ApiResponse(code=406,message="不是指定的数据类型"),
            @ApiResponse(code=500,message="服务器内部错误")
    })
    @RequestMapping(value="/findActivityOne", method= RequestMethod.POST)
    public  @ResponseBody
    ResponseBodyBean findActivityOne(@RequestBody PageData pd ) throws IOException, ParseException {
        int status = HttpStatus.INTERNAL_SERVER_ERROR.value();
        ResponseBodyBean responseBodyBean = new ResponseBodyBean();
        PageData pData = new PageData();
        PageData findActivityOne= activityService.findActivityOne(pd);
        //图片转集合
        String [] pic = findActivityOne.getString("activity_pic").split(",");
        List<PageData> picList = new ArrayList<PageData>() ;
        for(int i=0;i<pic.length;i++){
            picList.add(new PageData("picUrl",pic[i].toString()));
        }
        //计算时间差
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String remainingTime = TimeUtil.secondToTime(Long.parseLong(findActivityOne.get("remainingTime").toString()));
        findActivityOne.put("remainingTime",remainingTime);
        findActivityOne.put("activity_begtime",simpleDateFormat.format(simpleDateFormat.parse(findActivityOne.get("activity_begtime").toString())));
        findActivityOne.put("activity_endtime",simpleDateFormat.format(simpleDateFormat.parse(findActivityOne.get("activity_endtime").toString())));
        findActivityOne.put("picList" ,picList);
        pData.put("list", findActivityOne);
        status = HttpStatus.OK.value();
        responseBodyBean.setResult(pData);
        response.setStatus(status);
        return responseBodyBean;
    }
}
